#include "aggAux.h"

// Define the priority queue based on the boundaries and numb. of connectivities
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// IN: fineMesh
// OUT: seedQueue
vector<pEntity> sortSeedNodes(pMesh fineMesh)
{

	// Loop on nodes to define the priority of each one
	// Rule: At first should come all the boundary nodes, ordered according
	// to the number of connectivities and only after these the interior ones
	// according also to the number of connectivities
	vector<pEntity> nodePQ; // Container with the ordered list of nodes
	const int boundPriority(100);

    //(DEV)
    double coord[3] = {.0, .0, .0};
    int priority = 0;

	VIter vit = M_vertexIter(fineMesh);
	//cout<<"\n Nodes before sorting... \n";
	while (pEntity node = VIter_next(vit))
	{
    		int priority(0);
	    	// Priority is initially just the number of connectivities around the node
    		priority = V_numEdges(node);
            //cout<<"NumEdges: "<<V_numEdges(node)<<"...";

    		// Adding constant for higher priority for boundary nodes, as they must
    		// be treated first
            int isBoundary(0);
            EN_getDataInt(node, MD_lookupMeshDataId("isBoundary"), &isBoundary);
            //cout<<"isBoundary: "<<isBoundary<<"\n";

	    	if (isBoundary)
	    	{
    			priority += boundPriority;
	    	}

    		EN_attachDataInt(node, MD_lookupMeshDataId("priority"), priority);

	    	// Initializing agglomeration status to false
	    	int aggStatus(0);
    		EN_attachDataInt(node, MD_lookupMeshDataId("aggStatus"), aggStatus);

	    	// Adding node to the priority queue
            nodePQ.push_back(node);

	    	// Adding pq_item Location to the vector for future search

	    	// (DEV)
	    	coord[0] = 0.0;
	    	coord[1] = 0.0;
	    	coord[2] = 0.0;
            priority = 0;
	    	V_coord(node, coord);
            EN_getDataInt(node, MD_lookupMeshDataId("priority"), &priority);

            //std::cout<<"ID: "<<EN_id(node)<<" | Priority: "<< priority <<" | x: "<<coord[0]<<" | y: "<<coord[1]<<" | z: "<<coord[2]<<std::endl;

	}
	VIter_delete(vit);

    // Ordering the list ("make heap")
    make_heap(nodePQ.begin(), nodePQ.end(), CompareFMDB());

	return nodePQ;
}
